#### Dataframe 02-24-2023 #### 
packs <- c('dplyr', 'readstata13', 
           'stringr', 'estimatr', 'lubridate', 'mice', 
           'ggplot2', 'ggthemes')
lapply(packs, require, character.only = T)

setwd("~/Dropbox/Argentina Project/_effects_of_tj_replication")

#### 2012 LAPOP #### 

lapop12 = read.dta13("data/Argentina LAPOP AmericasBarometer 2012 Rev1_W.dta")  %>%   
  mutate(date = str_pad(fecha, 4, pad = '0'), 
         day = substring(date, 1,2), 
         month = substring(date, 3, 4), 
         year = 2012,
         daten = as.numeric(paste0(month, day)),
         ymd = as.Date(paste(year, month, day, sep = '-'),
                       orgin = "%Y-%m-%d"), 
         dow = wday(ymd, week_start=1, label=TRUE, abbr=FALSE), 
         weekend = as.numeric(dow %in% c('Saturday', 'Sunday'), 1,0), 
         
         trial = as.numeric(daten > 0326, 1,0)) %>% 
  group_by(ymd) %>% 
  mutate(
    trend = cur_group_id(), 
    n_survey = n()
  ) %>% 
  mutate(trend = trend-25, 
         torture_bin = as.numeric(vol205== 'No aprobaría ni entendería', 1,0), 
         
         soc.cl_bin = as.numeric(vol204== 'No aprobaría ni entendería', 1,0),
         
         soc.cl_lvl = recode(vol204, "No aprobaría ni entendería" = 3, 
                             'No aprobaría pero entendería' = 2, 
                             'Aprobaría' = 1
         ), 
         torture_lvl = recode(vol205, "No aprobaría ni entendería" = 3, 
                              'No aprobaría pero entendería' = 2, 
                              'Aprobaría' = 1
         ), 
         
         ideology = recode(l1, 'Derecha' = 10, 
                           'Izquierda' = 1),
         
         ideology = ifelse(is.na(ideology) == T,  l1, ideology), 
         
         coup_one = as.numeric(jc1 == 'Se justificaría que los militares tomen el poder por un golp', 1,0), 
         
         coup_two = as.numeric(jc10 == 'Se justificaría que los militares tomen el poder por un golp', 1,0), 
         
         coup_three = as.numeric(jc13 == 'Se justificaría que los militares tomen el poder por un golp', 1,0), 
         
         coup_four = as.numeric(jc15a == 'Sí se justifica', 1,0), 
         
         coup_five = as.numeric(jc16a == 'Sí se justifica', 1,0), 
         
         fair_trial = b1, 
         
         catholic = as.numeric(q3c == "Católico",1,0),
         
         
         nonrelig = as.numeric(q3c %in% c("Ninguna","Agnóstico o ateo"),1,0),
         
         
         militaryserv = as.numeric(mil8 %in% c("Sí, actualmente sirviendo","Servía en el pasado"),1,0),
         employed = as.numeric(ocup4a %in% c("Trabajando?","No está trabajando en este momento pero tiene trabajo?"),1,0),
         
         income =  recode(q10new, "Entre 770 - 1150" = 960, 
                          'Entre 1151 - 1530' = 1340.5, 
                          'Entre 1531 - 2300' = 1915.5,
                          'Entre 2301 - 3060' =  2680.5, 
                          'Entre 3061 - 3450' =  3255.5, 
                          'Entre 3451 - 3840' =  3645.5, 
                          'Entre 3841 - 4600' =  4220.5,
                          'Entre 4601 - 5750' =  5175.5, 
                          'Entre 5751 - 6900' =  6325.5, 
                          'Entre 6901 - 8050' =  7475.5, 
                          'Entre 8051 - 9200' = 8625.5, 
                          'Entre 9201 - 10350' = 9775.5, 
                          'Entre 10351 - 11500' = 10925.5,
                          'Entre 11501 - 13800' =  12650.5,
                          'Más de 13800' = 13800,
                          'Menos de 770' = 770, 
                          'Ningún ingreso' = 0 
         ),
         
         income_g =  recode(q10g, "Entre 770 - 1150" = 960, 
                            'Entre 1151 - 1530' = 1340.5, 
                            'Entre 1531 - 2300' = 1915.5,
                            'Entre 2301 - 3060' =  2680.5, 
                            'Entre 3061 - 3450' =  3255.5, 
                            'Entre 3451 - 3840' =  4220.5, 
                            'Entre 4601 - 5750' =  5175.5, 
                            'Entre 5751 - 6900' =  6325.5, 
                            'Entre 6901 - 8050' =  7475.5, 
                            'Entre 8051 - 9200' = 8625.5, 
                            'Entre 9201 - 10350' = 9775.5, 
                            'Entre 10351 - 11500' = 10925.5,
                            'Entre 11501 - 13800' =  12650.5,
                            'Más de 13800' = 13800,
                            'Menos de 770' = 770, 
                            'Ningún ingreso' = 0 
         ),
         
         married = as.numeric(q11 %in% c("Casado","Unión libre (juntado)"),1,0),
         white = as.numeric(etid == "Blanca",1,0),
         politknow = as.numeric(gi4 == "Correcto",1,0),
         computer = as.numeric(r15 == "Sí",1,0),
         
         single = as.numeric(q11=="Soltero", 1,0),
         
         internet_user = recode(www1, "Nunca" =5, 
                                'Diariamente' = 1, 
                                'Rara vez' =4, 
                                'Algunas veces a la semana' =2, 
                                'Algunas veces al mes' = 3
         ),
         
         news_poster = as.numeric(prot8 =="Sí, ha hecho", 1,0), 
         
         protest = as.numeric(prot3=="Sí ha participado", 1,0), 
         
         political_informed = recode(gi0, "Nunca" =5, 
                                     'Diariamente' = 1, 
                                     'Rara vez' =4, 
                                     'Algunas veces a la semana' =2, 
                                     'Algunas veces al mes' = 3), 
         
         reg.vote = as.numeric(vb1 == 'Sí', 1,0), 
         
         interest_pol = recode(pol1, 
                               'Nada' = 1, 
                               'Mucho' =4, 
                               'Algo' = 3,
                               'Poco' =2
         )
         
  )

lapop12$coup_support = as.numeric(
  rowSums(lapop12[328:332], na.rm = T) >0, 1,0
)

lapop12$income_pool = rowSums(lapop12[338:339], na.rm = T)

lapop12 = lapop12 %>% 
  mutate(
    ager = replace(q2, is.na(q2), median(q2, na.rm = T)), 
    agei = as.numeric(is.na(q2) == T, 1,0), 
    ager = replace(ager, is.na(ager), median(ager, na.rm = T)), 
    
  )
lapop12$ager[is.na(lapop12$ager)] = median(lapop12$ager, na.rm = T)

lapop_df = lapop12 %>%  
  ungroup() %>% 
  select('q1', 'ager', 'agei', 'ideology')

mice_imputed <- data.frame(
  original = lapop12$ideology,
  imputed_pmm = complete(mice(lapop_df, method = "pmm"))$ideology,
  imputed_cart = complete(mice(lapop_df, method = "cart"))$ideology,
  imputed_lasso = complete(mice(lapop_df, method = "lasso.norm"))$ideology
)

lapop12$ideolgyr = mice_imputed$imputed_pmm
lapop12$ideologyi = as.numeric(is.na(lapop12$ideology) == T, 1,0)
lapop12$ideolgyr = relevel(factor(lapop12$ideolgyr), ref = '5')

lapop12$fair_trial_impute = replace(lapop12$fair_trial, is.na(lapop12$fair_trial), 
                                    median(lapop12$fair_trial, na.rm = T)
)

lapop12 = lapop12 %>% 
  mutate(
    
    news = recode(gi0, "Nunca" = 1, 
                      'Diariamente' = 5, 
                      'Rara vez' = 2, 
                      'Algunas veces al mes' = 3, 
                     'Algunas veces a la semana' = 4
    )
    
  )
  


saveRDS(lapop12, 'data/lapop12.rds')